import type { RouterHistory } from './types'
import { normalizeBase } from './utils'

export function createWebHistory(base?: string): RouterHistory {
  // 1. 针对 base 做一个标准化操作
  base = normalizeBase(base)

  // 2. 创建导航对象
  const historyNavigation = useHistoryStateNavigation(base)

  // 3. 创建监听对象
  const historyListeners = useHistoryListeners(
    base,
    historyNavigation.state,
    historyNavigation.location
  )

  // 4. 将上面的对象合并到 routerHistory 的对象里面
  const routerHistory = Object.assign({}, historyNavigation, historyListeners)

  // 5. 为 routerHistory 添加 location 和 state 属性
  Object.defineProperty(routerHistory, 'location', {
    get: () => historyNavigation.location.value
  })
  Object.defineProperty(routerHistory, 'state', {
    get: () => historyNavigation.state.value
  })

  // 6. 返回 routerHistory
  return routerHistory
}

